export const WSNHistory = function(myZCloudID, myZCloudKey) {
	var thiz = this;
	thiz.uid = myZCloudID;
	thiz.key = myZCloudKey;
	thiz.saddr = "api.zhiyun360.com";
	
	thiz.setIdKey = function(uid, key) {
		thiz.uid = uid;
		thiz.key = key;		
	};
	
	thiz.initZCloud = function(uid, key) {
		thiz.uid = uid;
		thiz.key = key;		
	};
	
	thiz.setServerAddr = function(addr) {
		thiz.saddr = addr.replace('//', '')
		if (thiz.saddr.indexOf(':') > -1) {
			thiz.saddr = thiz.saddr.split(':').filter(item => {
				return item.length > 10 && item
			})[0]
		}
	};
	thiz.query = function(channel, start, end, interval, cb) {
		var url,q;
		if (arguments.length == 1) {
		    url = "http://"+thiz.saddr+":8080"+"/v2/feeds/"+thiz.uid;
		   cb = arguments[0];
		} else if (arguments.length == 2) {
			url = "http://"+thiz.saddr+":8080"+"/v2/feeds/"+thiz.uid+"/datastreams/"+channel;
			cb = arguments[1];
		} else if (arguments.length == 4) {
			q = "start="+start+"&end="+end;
			url = "http://"+thiz.saddr+":8080"+"/v2/feeds/"+thiz.uid+"/datastreams/"+channel+"?"+q;
			cb = arguments[3];
		} else if (arguments.length == 5) {
			q = "start="+start+"&end="+end+"&interval="+interval;
			url = "http://"+thiz.saddr+":8080"+"/v2/feeds/"+thiz.uid+"/datastreams/"+channel+"?"+q;
			cb = arguments[4];
		} else {
		   return;
		}
		/* uni-app版 */
		uni.request({
			method: "GET",
			url: url,
			header: {
				'X-ApiKey': thiz.xKey
			},
			success: function (res) {
				cb(res.data);
			}
		});
	};
	thiz.queryLast = function(channel, cb, pa) {
		var url = "http://"+thiz.saddr+":8080"+"/v2/feeds/"+thiz.uid+"/datastreams/"+channel;
		if (pa) {
			url += "?"+pa;
		}
		/* uni-app版 */
		uni.request({
			method: "GET",
			url: url,
			header: {
				'X-ApiKey': thiz.xKey
			},
			success: function (res) {
				cb(res.data);
			}
		});
	};
	thiz.queryLast1H = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=1hour");
	};
	thiz.queryLast6H = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=6hours");
	};
	thiz.queryLast12H = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=12hours");
	};
	thiz.queryLast1D = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=1day");
	};
	thiz.queryLast5D = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=5days");
	};
	thiz.queryLast14D = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=14days");
	};
	thiz.queryLast1M = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=1month");
	};
	thiz.queryLast3M = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=3months");
	};
	thiz.queryLast6M = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=6months");
	};
	thiz.queryLast1Y = function(channel, cb) {
		thiz.queryLast(channel, cb, "duration=1year");
	};
}

